package com.mightybird.designpattern.behavioral.command.command.impl;

import com.mightybird.designpattern.behavioral.command.command.AbstractCommand;
import com.mightybird.designpattern.behavioral.command.receiver.Adder;

import java.util.ArrayDeque;
import java.util.Deque;

public class Command extends AbstractCommand {
    private Adder adder = new Adder();
    private Deque<Integer> undoStack = new ArrayDeque<>();
    private Deque<Integer> redoStack = new ArrayDeque<>();

    @Override
    public int execute(int value) {
        undoStack.push(value);
        redoStack.clear();
        return adder.add(value);
    }

    @Override
    public int undo() {
        int n;
        if (undoStack.isEmpty()) {
            n = 0;
        } else {
            n = undoStack.pop();
            redoStack.push(n);
        }
        return adder.add(-n);
    }

    @Override
    public int redo() {
        int n;
        if (redoStack.isEmpty()) {
            n = 0;
        } else {
            n = redoStack.pop();
            undoStack.push(n);
        }
        return adder.add(n);
    }
}
